package site.assad.i18n;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/1 14:14
 * Description:在资源中使用格式化字符串
 */
public class FormatResourceBoundleTest {
    public static void main(String[] args){

        /*在资源中使用格式化字符串*/

        //加载本地化资源
        ResourceBundle resourceBundle1 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.US);
        ResourceBundle resourceBundle2 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.CHINA);

        //动态替换占位符参数
        Object[] params = {"Al-assad",new Date()};

        //获取本地化键值，并对其进行格式化
        String common1 = new MessageFormat(resourceBundle1.getString("greeting.common"),Locale.US).format(params);
        String morning1 = new MessageFormat(resourceBundle1.getString("greeting.morning"),Locale.US).format(params);
        String afternoon1 = new MessageFormat(resourceBundle1.getString("greeting.afternoon"),Locale.US).format(params);

        String common2 = new MessageFormat(resourceBundle2.getString("greeting.common"),Locale.CHINA).format(params);
        String morning2 = new MessageFormat(resourceBundle2.getString("greeting.morning"),Locale.CHINA).format(params);
        String afternoon2 = new MessageFormat(resourceBundle2.getString("greeting.afternoon"),Locale.CHINA).format(params);

        System.out.println(common1+"\n"+morning1+"\n"+afternoon1+"\n"+common2+"\n"+morning2+"\n"+afternoon2);
        /*output:
        How are you!Al-assad,today is 12/1/17
        Good morning!Al-assad,now is 2:22 PM
        Good afternoon!Al-assad,now is2:22 PM
        你好!Al-assad,今天是 17-12-1
        上午好!Al-assad,现在的时间是 下午2:22
        下午好!Al-assad,现在的时间是 下午2:22
        * */



    }
}

